package com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.BannersAdapter;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.Utils;
import com.diudiutechnology.www.yadiudiu.util.view.MyScrollView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.GoodsActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/21.
 * 干洗店铺详情
 */

public class DryCleaningShopDetailsActivity extends Activity implements View.OnClickListener, MyScrollView.Callbacks {
    @Bind(R.id.retur)//返回
            LinearLayout retur;
    @Bind(R.id.scroll_view)
    MyScrollView scrollView;
    @Bind(R.id.fixed_position)//固定goodsAppraise位置
            LinearLayout fixedPosition;
    @Bind(R.id.goods_appraise)
    LinearLayout goodsAppraise;


    @Bind(R.id.goods_layout)
    RelativeLayout goodsLayout;
    @Bind(R.id.all_goods)
    ImageView allGoods;
    @Bind(R.id.appraise_layout)
    RelativeLayout appraiseLayout;
    @Bind(R.id.appraise_img)
    ImageView appraiseImg;


    @Bind(R.id.viewPager)//商品和评价
            ViewPager viewPager;

    @Bind(R.id.ad_pager)//轮播
            ViewPager adPager;
    @Bind(R.id.ad_layer)//圈
            LinearLayout adLayer;
    private final int HOME_AD_RESULT = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 广告
                case HOME_AD_RESULT:
                    adPager.setCurrentItem(adPager.getCurrentItem() + 1,
                            true);
                    break;
            }
        }

        ;
    };
    Context context;
    LocalActivityManager manager = null;
    int pos = 0;//初始化scrollView高度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dry_cleaning_shop_details);
        ButterKnife.bind(this);
        context = this;
        manager = new LocalActivityManager(this, true);
        manager.dispatchCreate(savedInstanceState);
        initView();
    }

    private void initView() {


        retur.setOnClickListener(this);
        processviewPager();
        scrollView.setCallbacks(this);
        scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (pos < 2) {
                    scrollView.scrollTo(0, 0);
                    pos++;
                }
                System.out.println("scrollView.getScrollY()" + scrollView.getScrollY());
                onScrollChanged(scrollView.getScrollY());
            }
        });

        List<String> getinforresult = new ArrayList<>();
        getinforresult.add("1");
        getinforresult.add("1");
        getinforresult.add("1");
        getadvertising(getinforresult.size(), getinforresult);
    }

    private void processviewPager() {
        // 关闭预加载，默认一次只加载一个
        final ArrayList<View> list = new ArrayList<View>();
        Intent intent = new Intent(this,
                GoodsActivity.class);
        list.add(getView("GoodsActivity", intent));
        Intent intent2 = new Intent(this, ShopEvaluationActivity.class);

        list.add(getView("ShopEvaluationActivity", intent2));
        viewPager.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                Utils.getScreenHeight(context)));
        viewPager.setAdapter(new MyPagerAdapter(list));
        viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        viewPager.setCurrentItem(0);
        goodsLayout.setOnClickListener(new MyOnClickListener(0));
        appraiseLayout.setOnClickListener(new MyOnClickListener(1));
    }

    //轮播广告
    public void getadvertising(final int size, final List<String> getinforresult) {
        adPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Sundry.refreshPoint(adLayer, position % size, 0);
                if (mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
                mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (ViewPager.SCROLL_STATE_DRAGGING == arg0
                        && mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
            }
        });
        BannersAdapter adapter = new BannersAdapter(context, getinforresult, 0);
        adPager.setAdapter(adapter);
        Sundry.initPointsLayout(adLayer, context, size, 0);
        adPager.setCurrentItem(size * 1000, false);
        // 自动轮播线程
        mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
        }
    }

    @Override
    public void onScrollChanged(int scrollY) {
        goodsAppraise.setTranslationY(Math.max(fixedPosition.getTop(), scrollY));
    }

    @Override
    public void onDownMotionEvent() {

    }

    @Override
    public void onUpOrCancelMotionEvent() {

    }

    /**
     * 通过activity获取视图
     *
     * @param id
     * @param intent
     * @return
     */
    private View getView(String id, Intent intent) {
        return manager.startActivity(id, intent).getDecorView();
    }

    /**
     * Pager适配器
     */
    public class MyPagerAdapter extends PagerAdapter {
        List<View> list = new ArrayList<View>();

        public MyPagerAdapter(ArrayList<View> list) {
            this.list = list;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ViewPager pViewPager = ((ViewPager) container);
            pViewPager.removeView(list.get(position));
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ViewPager pViewPager = ((ViewPager) arg0);
            pViewPager.addView(list.get(arg1));
            return list.get(arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }

    /**
     * 页卡切换监听
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            System.out.println("arg0=" + arg0);

            switch (arg0) {
                case 0:
                    allGoods.setVisibility(View.VISIBLE);
                    appraiseImg.setVisibility(View.GONE);
                    break;
                case 1:
                    allGoods.setVisibility(View.GONE);
                    appraiseImg.setVisibility(View.VISIBLE);
                    break;
            }

        }

        @Override
        public void onPageScrollStateChanged(int pos) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }
    }

    /**
     * 2      * 头标点击监听
     * 3
     */
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(index);
        }
    }
}
